package com.lili.operate.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lili.common.result.Result;
import com.lili.model.operate.Message;
import com.lili.model.operate.MessageSign;
import com.lili.model.operate.MessageTemplate;
import com.lili.model.operate.NoticeMessage;
import com.lili.operate.service.MessageService;
import com.lili.operate.service.MessageSignService;
import com.lili.operate.service.MessageTemplateService;
import com.lili.operate.service.NoticeMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin/sms")
@Api(tags = "短信服务模块")
public class MessageController {
    @Autowired
    MessageSignService messageSignService;

    @Autowired
    MessageTemplateService messageTemplateService;

    @Autowired
    MessageService messageService;

    @Autowired
    NoticeMessageService noticeMessageService;


    @ApiOperation("根据id修改站内信的开启状态")
    @PostMapping("/update/notice/status")
    public Result updateNoticeStatus(@RequestBody Map map){
        long id = Long.parseLong(map.get("id").toString());
        String noticeStatus = map.get("noticeStatus").toString();
        int rs = noticeMessageService.updateStatus(id,noticeStatus);
        return rs==1 ? Result.ok() : Result.fail();
    }

    @ApiOperation("修改通知类站内信")
    @PutMapping("/update/notice")
    public Result updateNoticeMessage(@RequestBody NoticeMessage noticeMessage){
        int rs = noticeMessageService.updateNoticeMessage(noticeMessage);
        return rs==1 ? Result.ok() : Result.fail();
    }

    /**
     * 分页查询通知类站内信
     * @param map
     * @return
     */
    @ApiOperation("查询通知类站内信")
    @PostMapping("/query/notices")
    public Result queryNoticesMessage(@RequestBody Map map){
        Page<NoticeMessage> page = noticeMessageService.queryNoticeMessage(map);
        return Result.ok(page);
    }



    /**
     * 保存短信
     * @param message
     * @return
     */

    @PostMapping("/save/message")
    public Result saveMessage(@RequestBody Message message){
        int rs = messageService.saveMessage(message);
        return rs==1?Result.ok():Result.fail();
    }


    /**
     * 短信模板
     * @param messageTemplate
     * @return
     */

    @PutMapping("/update/template")
    public Result updateTemplate(@RequestBody MessageTemplate messageTemplate){
        int rs = messageTemplateService.updateTemplate(messageTemplate);
        return rs==1?Result.ok():Result.fail();
    }


    @ApiOperation("保存短信模板")
    @PostMapping("/save/template")
    public  Result saveTemplate(@RequestBody MessageTemplate messageTemplate){
        int rs = messageTemplateService.saveTemplate(messageTemplate);
        return rs==1?Result.ok():Result.fail();
    }

    @ApiOperation("保存短信签名")
    @PostMapping("/save/sign")
    public Result saveMessageSign(@RequestBody MessageSign messageSign){
        int rs = messageSignService.saveMessageSign(messageSign);
        return rs==1 ? Result.ok() : Result.fail();
    }

    @ApiOperation("查询所有的短信签名")
    @PostMapping("/query/signs")
    public Result querySigns(@RequestBody Map map){
        int current = 1;
        int size = 2;
        if(map.get("current")!=null){
            current = Integer.parseInt(map.get("current").toString());
        }
        if(map.get("current")!=null){
            size = Integer.parseInt(map.get("size").toString());
        }
        Page<MessageSign> page = messageSignService.queryByPage(current,size);
        return page==null?Result.fail():Result.ok(page);
    }

    @ApiOperation("更新短信签名")
    @PutMapping("/update/sign")
    public Result updateSign(@RequestBody MessageSign messageSign){
        int rs = messageSignService.updateSign(messageSign);
        return rs==1?Result.ok():Result.fail();
    }

    @ApiOperation("删除Sign")
    @DeleteMapping("/delete/sign")
    public  Result deleteSign(@RequestParam long id){
        int rs = messageSignService.deleteSign(id);
        return rs==1?Result.ok():Result.fail();
    }


}
